package com.zhl.config;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Iterator;
import java.util.Map;

public class ApplicationContextIdConditional extends SpringBootCondition {
    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Map annotationAttributes = metadata.getAnnotationAttributes(ApplicationContextIdCondition.class.getName());
        Object value = annotationAttributes.get("value");
        if(value == null){
            return new ConditionOutcome(false, "error");
        }

        Iterator<String> beanNameIterator = context.getBeanFactory().getBeanNamesIterator();
        while(beanNameIterator.hasNext()) {
            System.out.println(beanNameIterator.next());
        }
//        String aaa = context.getBeanFactory().getBeanDefinition(SpringApplication.class.getName()).getScope();
//        String applicationContextId = "";

//        if (value.equals(applicationContextId)) {
//            //如果environment中的值与指定的value一致，则返回true
//            return new ConditionOutcome(true, "ok");
//        }

        if (context.getBeanFactory().getBeanDefinition("LocalFeignMark") != null) {
            return new ConditionOutcome(true, "ok");
        }

        return new ConditionOutcome(false, "error");

//        return expectedId.equals(context.getBeanFactory().getBeanDefinition(SpringApplication.class.getName()).getScope());
    }
}
